<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="姓名" prop="memberName">
        <el-input
          v-model="queryParams.memberName"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--       -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="memberInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="姓名" align="center" prop="memberName" />
<!--      <el-table-column label="人像" align="center" prop="member_url" />-->
<!--      <el-table-column label="年龄" align="center" prop="age" />-->
      <el-table-column label="性别" align="center" prop="sex" >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.sex == 0" type="success">男</el-tag>
          <el-tag v-else-if="scope.row.sex == 1" type="danger">女</el-tag>
          <el-tag v-else type="info">未知</el-tag>
        </template>
      </el-table-column>
<!--      <el-table-column label="部门" align="center" prop="deptName" />-->
<!--      <el-table-column label="学历" align="center" prop="edu" />-->
<!--      <el-table-column label="毕业院校" align="center" prop="school" />-->
<!--      <el-table-column label="成员类型" align="center" prop="memberType" />-->
<!--      <el-table-column label="身高 cm" align="center" prop="height" />-->
      <el-table-column label="上班状态" align="center" prop="status" >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status == 'WORKING'" type="success">工作中</el-tag>
          <el-tag v-else-if="scope.row.sex == 'ASK_LEAVE'" type="danger">请假</el-tag>
          <el-tag v-else type="info">离职</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="简介" align="center" prop="brief" />
      <el-table-column label="负责房间" align="center" prop="room" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--          >删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改成员信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body center>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
<!--        <el-form-item label="部门" prop="deptId">-->
<!--          <el-input v-model="form.deptId" placeholder="请输入所在部门" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="${comment}" prop="userId">-->
<!--          <el-input v-model="form.userId" placeholder="请输入${comment}" />-->
<!--        </el-form-item>-->
        <el-form-item label="姓名" prop="memberName">
          <el-input v-model="form.memberName" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-select v-model="form.sex" clearable placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="上班状态" prop="status">
          <el-select v-model="form.status" clearable placeholder="请选择">
            <el-option
              v-for="item in workStatus"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
<!--        <el-form-item label="学历" prop="edu">-->
<!--          <el-input v-model="form.edu" placeholder="请输入学历" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="毕业院校" prop="school">-->
<!--          <el-input v-model="form.school" placeholder="请输入毕业院校" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="年龄" prop="age">-->
<!--          <el-input v-model="form.age" placeholder="请输入年龄" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="身高cm" prop="height">-->
<!--          <el-input v-model="form.height" placeholder="请输入身高 cm" />-->
<!--        </el-form-item>-->
        <el-form-item label="简介" prop="brief">
          <el-input v-model="form.brief" placeholder="请输入简介" />
        </el-form-item>
        <el-form-item label="负责房间" prop="room">
          <el-select v-model="form.room" filterable  multiple placeholder="请选择">
            <el-option
              v-for="item in nurseRoomInfoList"
              :key="item.roomId"
              :label="item.roomCode +':'+item.roomSex"
              :value="item.roomId">
            </el-option>
          </el-select>
        </el-form-item>
<!--        <el-form-item label="${comment}" prop="createUserId">-->
<!--          <el-input v-model="form.createUserId" placeholder="请输入${comment}" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="${comment}" prop="updateUserId">-->
<!--          <el-input v-model="form.updateUserId" placeholder="请输入${comment}" />-->
<!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMemberInfo, getMemberInfo, delMemberInfo, addMemberInfo, updateMemberInfo } from "@/api/business/memberInfo";
import {getNurseRoomInfo} from "@/api/business/roomInfo";

export default {
  name: "MemberInfo",
  data() {
    return {
      options: [{
        value: '0',
        label: '男'
      }, {
        value: '1',
        label: '女'
      }, {
        value: '2',
        label: '未知'
      }],
      workStatus: [{
        value: 'WORKING',
        label: '工作中'
      }, {
        value: 'ASK_LEAVE',
        label: '请假'
      }],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 成员信息表格数据
      memberInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deptId: null,
        userId: null,
        memberName: null,
        edu: null,
        school: null,
        memberType: null,
        age: null,
        sex: null,
        height: null,
        status: null,
        brief: null,
        createUserId: null,
        updateUserId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      nurseRoomInfoList:{},
    };
  },
  created() {
    this.getList();
    this.getNurseRoom();
  },
  methods: {
    /** 查询成员信息列表 */
    getList() {
      this.loading = true;
      listMemberInfo(this.queryParams).then(response => {
        this.memberInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        memberId: null,
        deptId: null,
        userId: null,
        memberName: null,
        edu: null,
        school: null,
        memberType: null,
        age: null,
        sex: null,
        height: null,
        status: null,
        brief: null,
        createUserId: null,
        createTime: null,
        updateUserId: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.memberId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加成员信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const memberId = row.memberId || this.ids
      getMemberInfo(memberId).then(response => {
        this.form = response.data;
        console.log(this.form);
        this.open = true;
        this.title = "修改成员信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.memberId != null) {
            console.log(this.form);
            updateMemberInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          // } else {
          //   addMemberInfo(this.form).then(response => {
          //     this.$modal.msgSuccess("新增成功");
          //     this.open = false;
          //     this.getList();
          //   });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const memberIds = row.memberId || this.ids;
      this.$modal.confirm('是否确认删除成员信息编号为"' + memberIds + '"的数据项？').then(function() {
        return delMemberInfo(memberIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    // handleExport() {
    //   this.download('business/memberInfo/export', {
    //     ...this.queryParams
    //   }, `memberInfo_${new Date().getTime()}.xlsx`)
    // },
    getNurseRoom(){
      getNurseRoomInfo().then(response => {
        this.nurseRoomInfoList = response.rows;
        console.log(this.nurseRoomInfoList);
      })
    }
  }
};
</script>
